/**
 *
 */
package com.zhuoyue.elmt.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eit.eitsite.modules.sys.utils.UserUtils;
import com.zhuoyue.elmt.entity.DeviceParameters;
import com.zhuoyue.elmt.entity.MaintenanceContractDevice;
import com.zhuoyue.elmt.service.CsDtcsService;
import com.zhuoyue.elmt.service.GgSbjcService;
import com.zhuoyue.elmt.service.MaintenanceContractDeviceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.elmt.entity.MaintenanceContract;
import com.zhuoyue.elmt.service.MaintenanceContractService;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 维保合同备案Controller
 *
 * @author chenhao
 * @version 2016-09-08
 */
@Controller
@RequestMapping(value = "${adminPath}/elmt/maintenanceContract")
public class MaintenanceContractController extends BaseController {

    @Autowired
    private MaintenanceContractService maintenanceContractService;

    @Autowired
    private GgSbjcService ggSbjcService;

    @Autowired
    private CsDtcsService csDtcsService;

    @Autowired
    private MaintenanceContractDeviceService maintenanceContractDeviceService;

    @ModelAttribute
    public MaintenanceContract get(@RequestParam(required = false) String id) {
        MaintenanceContract entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = maintenanceContractService.get(id);
        }
        if (entity == null) {
            entity = new MaintenanceContract();
        }
        return entity;
    }

    @RequiresPermissions("elmt:maintenanceContract:view")
    @RequestMapping(value = {"list", ""})
    public String list(MaintenanceContract maintenanceContract, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (!UserUtils.getUser().isAdmin()) {
            maintenanceContract.setMaintenanceUnitId(UserUtils.getUser().getCompany().getCode());
        }

        Page<MaintenanceContract> page = maintenanceContractService.findPage(new Page<MaintenanceContract>(request, response), maintenanceContract);
        model.addAttribute("page", page);
        return "zhuoyue/elmt/maintenanceContractList";
    }

    @RequiresPermissions("elmt:maintenanceContract:view")
    @RequestMapping(value = "form")
    public String form(MaintenanceContract maintenanceContract, Model model) {
        model.addAttribute("maintenanceContract", maintenanceContract);
        MaintenanceContractDevice record = new MaintenanceContractDevice();
        record.setContractNo(maintenanceContract.getId());
        List<HashMap> list = maintenanceContractService.findContractDeviceList(record);
        model.addAttribute("list", list);
        return "zhuoyue/elmt/maintenanceContractForm";
    }

    @RequiresPermissions("elmt:maintenanceContract:view")
    @RequestMapping(value = "iframeDeviceAll")
    public String iframeDeviceAll(MaintenanceContract maintenanceContract, Model model) {
        model.addAttribute("maintenanceContract", maintenanceContract);
        MaintenanceContractDevice record = new MaintenanceContractDevice();
        record.setContractNo(maintenanceContract.getId());
        if (!UserUtils.getUser().isAdmin()) {
            record.setRemark(UserUtils.getUser().getCompany().getCode());//维保单位编码
        }
        List<HashMap> list = maintenanceContractService.findContractDeviceList(record);
        model.addAttribute("list", list);
        model.addAttribute("listszie", list.size());
        return "zhuoyue/elmt/iframeDeviceList";
    }

    @RequiresPermissions("elmt:maintenanceContract:edit")
    @RequestMapping(value = "save")
    public String save(String sbzcdm, MaintenanceContract maintenanceContract, HttpServletRequest request, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, maintenanceContract)) {
            return form(maintenanceContract, model);
        }
        if (StringUtils.isEmpty(maintenanceContract.getId())) {
            maintenanceContract.setMaintenanceUnitId(UserUtils.getUser().getCompany().getCode());
            maintenanceContract.setMaintenanceUnitName(UserUtils.getUser().getCompany().getName());
            maintenanceContract.setCreatedBy((UserUtils.getUser().getName()));
            maintenanceContract.setCreationDate(new Date());
        }
        maintenanceContractService.save(maintenanceContract);
        if (StringUtils.isNotEmpty(sbzcdm)) {
            MaintenanceContractDevice maintenanceContractDevice = new MaintenanceContractDevice();
            maintenanceContractDevice.setId(maintenanceContract.getId());
            maintenanceContractDevice.setContractNo(maintenanceContract.getContractNo());
            maintenanceContractDevice.setDeviceCode(sbzcdm);
            maintenanceContractDeviceService.update(maintenanceContractDevice);
        }
        addMessage(redirectAttributes, "保存维保合同信息成功");
        redirectAttributes.addAttribute("id", maintenanceContract.getId());
        return "redirect:" + Global.getAdminPath() + "/elmt/maintenanceContract/form?repage";
    }

    @RequiresPermissions("elmt:maintenanceContract:edit")
    @RequestMapping(value = "delete")
    public String delete(MaintenanceContract maintenanceContract, RedirectAttributes redirectAttributes) {
        maintenanceContractService.delete(maintenanceContract);
        addMessage(redirectAttributes, "删除维保合同信息成功");
        return "redirect:" + Global.getAdminPath() + "/elmt/maintenanceContract/?repage";
    }

    @RequiresPermissions("elmt:maintenanceContract:edit")
    @RequestMapping(value = "modifyEquipmentParameter")
    public String modifyEquipmentParameter(DeviceParameters deviceParameters, Model model, RedirectAttributes redirectAttributes) {
        ggSbjcService.modifyGgsbjcParameter(deviceParameters);
        csDtcsService.modifyCsDtcsParameter(deviceParameters);
        addMessage(redirectAttributes, "保存保存设备参数成功");
        redirectAttributes.addAttribute("tzsbbh", deviceParameters.getTzsbbh());
        return "redirect:" + Global.getAdminPath() + "/elmt/deviceParameters/getEquipmentParameter/?repage";
    }

    @RequiresPermissions("elmt:maintenanceContract:edit")
    @RequestMapping(value = "chooseEquipment")
    public String chooseEquipment(MaintenanceContract maintenanceContract, HttpServletRequest request, Model model, RedirectAttributes redirectAttributes) {
        request.getSession().setAttribute("MaintenanceContract", maintenanceContract);
        MaintenanceContractDevice record = new MaintenanceContractDevice();
        record.setContractNo(maintenanceContract.getId());
        List<HashMap> list = maintenanceContractService.findContractDeviceList(record);
        model.addAttribute("list", list);
        return "zhuoyue/elmt/deviceList";
    }
}